<template>
    <div>
        <el-row :gutter="20">
            <el-col :span="8">
                <el-card shadow="hover" class="mgb20" style="height:240px;">
                    <div class="user-info">
                        <el-image :src="avatarUrl" class="user-avator" :preview-src-list="[avatarUrl]"></el-image>
                        <div class="user-info-cont">
                            <div class="user-info-name">{{name}}</div>
                            <div>{{role}}</div>
                        </div>
                    </div>
                    <div class="user-info-list">
                        上次登录时间：
                        <span>{{currentUser.lastLogin}}</span>
                    </div>
                </el-card>
            </el-col>
            <el-col :span="16">
                <el-card shadow="hover" style="height:403px;">
                    <div slot="header" class="clearfix">
                        <el-badge :value="total">
                            <el-button>新消息通知</el-button>
                        </el-badge>
                        <el-button style="float: right; padding: 3px 0" type="text" @click="handleAllRead">全部已读
                        </el-button>
                    </div>
                    <el-table
                            :show-header="false"
                            :data="messageList"
                            :row-class-name="tableRowClassName"
                            highlight-current-row
                            @current-change="handleCurrentChange"
                            style="width:100%;">
                        <el-table-column type="index" width="50"></el-table-column>
                        <el-table-column prop="details" show-tooltip-when-overflow></el-table-column>
                        <el-table-column prop="createTime"></el-table-column>
                        <el-table-column prop="type"></el-table-column>
                        <el-table-column width="60">
                            <template slot-scope="scope">
                                <el-button type="text"
                                           @click="handleDelete(scope.row.messageId, scope.$index)"
                                           style="color: #97a8be">
                                    <i class="el-icon-delete"></i>
                                </el-button>
                            </template>
                        </el-table-column>
                    </el-table>
                </el-card>
            </el-col>
        </el-row>

        <!--消息详情-->
        <el-dialog
                title="消息详情"
                :visible.sync="showDetails"
                width="30%"
                @close="handleClose"
                top="15%"
                center>
            <span>{{this.details}}</span>
        </el-dialog>
    </div>
</template>

<script>

    import { getRequest, postRequest } from '../../api';
    import bus from '../common/bus';

    export default {
        name: 'dashboard',
        data() {
            return {
                name: sessionStorage.getItem('ms_username'),
                noAvatar: 'https://cube.elemecdn.com/3/7c/3ea6beec64369c2642b92c6726f1epng.png',
                currentUser: {},
                messageList: [],
                total: 0,
                showDetails: false,
                details: '',
                messageId: 0,
                idx: 0
            };
        },
        components: {},
        watch: {
          messageList(){
              this.getMessageCount();
          }
        },
        computed: {
            role() {
                return this.name === 'admin' ? '超级管理员' : '普通管理员';
            },
            avatarUrl() {
                let avatar = sessionStorage.getItem('avatar');
                return avatar !== 'null' ? 'http://localhost:8888' + avatar : this.noAvatar;
            }
        },
        methods: {
            getCurrentUser() {
                let user = JSON.parse(sessionStorage.getItem('currentUser'));
                this.currentUser = user;
                const params = { current: 1, size: 10 };
                getRequest('/getSystemMessageNotRead', params).then(res => {
                    if (res) {
                        this.messageList = res.list;
                        this.total = res.total;
                    }
                });
            },
            handleCurrentChange(row, index){
                this.details=row.details;
                this.messageId=row.messageId;
                this.idx=index;
                this.showDetails=true;
            },
            handleClose(){
                this.handleDelete(this.messageId, this.idx);
                this.showDetails=false;
            },
            handleDelete(messageId, idx) {
                const params = { messageId: messageId };
                getRequest('/processRead', params).then(res => {
                    if (res) {
                        this.$message.success('消息已设为已读！');
                        this.messageList.splice(idx, 1);
                        this.total = this.total - 1;
                    }
                });
            },
            tableRowClassName({ row, rowIndex }) {
                if (row.isRead === 0) {
                    return 'warning-row';
                }
            },
            handleAllRead() {
                let messageIdList = [];
                this.messageList.forEach(item => {
                    messageIdList.push(item.messageId);
                });
                if (messageIdList.length === 0) {
                    return false;
                }
                postRequest('/multipartProcessRead', { messageIdList: messageIdList }).then(res => {
                    if (res === 'success') {
                        this.$message.success('消息已全部设置为已读！');
                        this.messageList = [];
                        this.total = this.total - messageIdList.length;
                    }
                });
            },
            getMessageCount(){
              bus.$emit('messageCount', this.messageList.length);
            }
        },
        mounted() {
            this.getCurrentUser();
        }
    };
</script>


<style scoped>
    .user-info {
        display: flex;
        align-items: center;
        padding-bottom: 20px;
        border-bottom: 2px solid #ccc;
        margin-bottom: 20px;
    }

    .user-avator {
        width: 120px;
        height: 120px;
        border-radius: 50%;
    }

    .user-info-cont {
        padding-left: 50px;
        flex: 1;
        font-size: 14px;
        color: #999;
    }

    .user-info-cont div:first-child {
        font-size: 30px;
        color: #222;
    }

    .user-info-list {
        font-size: 14px;
        color: #999;
        line-height: 25px;
    }

    .user-info-list span {
        margin-left: 70px;
    }

    .mgb20 {
        margin-bottom: 20px;
    }

    .el-table {
        font-size: 14px;
    }

    /deep/ .el-table .warning-row {
        background: oldlace;
    }
</style>
